package com.qf.Dao.impl;

import com.qf.Dao.PatientHistoryDao;
import com.qf.Utils.DBUtils;
import com.qf.entity.PatientHistory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class PatientHistoryDaoImpl implements PatientHistoryDao {
    private QueryRunner qr = new QueryRunner(DBUtils.DB.getDs());

    @Override
    public List<PatientHistory> selectByPatientName(String patientName) throws SQLException {
        String sql = "SELECT * FROM patient_history as p WHERE patient_Name = ?";
        List<PatientHistory> ByPatientName = qr.query(sql, new BeanListHandler<PatientHistory>(PatientHistory.class), patientName);
        return ByPatientName;
    }

    @Override
    public int addPatientName(PatientHistory patientHistory) throws SQLException {
        String sql = "INSERT INTO patient_history (id,patient_idcard,patient_name,patient_sym,patient_doctor,patient_medicine,patient_history_date,takeprice) VALUES(?,?,?,?,?,?,?,?)";
        Object[] objects = {patientHistory.getId(), patientHistory.getPatient_Idcard(), patientHistory.getPatient_Name(), patientHistory.getPatient_Sym(), patientHistory.getPatient_Doctor(), patientHistory.getPatient_Medicine(), patientHistory.getPatient_History_Date(), patientHistory.getTakeprice()};
        return qr.update(sql, objects);
    }

    @Override
    public int updatePatientName(PatientHistory patientHistory) throws SQLException {
        String sql = "UPDATE patient_history SET id=?,patient_sym=?,patient_doctor=?,patient_medicine=?,patient_history_date=?,takeprice=? WHERE patient_Name = ? AND patient_idcard = ?";
        Object[] objects = {patientHistory.getId(), patientHistory.getPatient_Sym(), patientHistory.getPatient_Doctor(), patientHistory.getPatient_Medicine(), patientHistory.getPatient_History_Date(), patientHistory.getTakeprice(), patientHistory.getPatient_Name(), patientHistory.getPatient_Idcard()};
        return qr.update(sql, objects);
    }

    @Override
    public int deletePatientName(String patientName, String patient_Idcard) throws SQLException {
        String sql = "DELETE FROM patient_history WHERE patient_Name = ? AND patient_idcard = ?";
        Object[] objects = {patientName, patient_Idcard};
        return qr.update(sql, objects);
    }
}
